home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
cpp_libs
/
array.lha
/
array
/
array.C
next >
Wrap
Text File
|
1993-08-08
|
2KB
|
88 lines
/ Implementation of template array class, fixed size.
/
/ Assertions can be turned off by defining NDEBUG, see assert(3).
/
/ Author: Dag Bruck, Department of Automatic Control, Lund Institute of
/ Technology, Box 188, S-221 00 Lund, Sweden. E-mail: dag@control.lth.se
/
/ $Id: array.C,v 1.6 1992/07/07 12:39:02 dag Exp $
include <array.H>
include <assert.h>
emplate <class T>
rray<T>::Array(int l, int h)
: low(l), sz(h-l+1)
assert(h >= l);
data = new T[sz];
assert(data != 0);
emplate <class T>
rray<T>::~Array()
delete [] data;
emplate <class T>
& Array<T>::operator [] (int i)
assert(i >= low);
unsigned index = i - low;
assert(index < sz);
return data[index];
emplate <class T>
onst T& Array<T>::operator [] (int i) const
return ((Array<T>*) this)->operator [] (i);
emplate <class T>
rray<T>& Array<T>::operator = (const T& x)
for (unsigned i = 0; i < sz; i++) data[i] = x;
return *this;
emplate <class T>
rray<T>& Array<T>::operator = (const Array<T>& a)
if (this != &a) {
assert(low == a.low && sz == a.sz);
for (unsigned i = 0; i < sz; i++) data[i] = a.data[i];
}
return *this;
emplate <class T>
oid Array<T>::bounds(int& lower_bound, int& upper_bound) const
lower_bound = low;
upper_bound = low + sz - 1;
ifdef TEST
include <stream.h>
truct Foo {
Foo() : x(0) { cout << "making a Foo @ " << (void*) this << endl; }
Foo(const Foo& f) : x(f.x) {}
void operator=(const Foo& f) { x = f.x; }
~Foo() { cout << "destroying a Foo @ " << (void*) this << endl; }
int x;
;
ain()
Foo f;
Array<Foo> a(1, 5);
a = f;
Array<Foo> b(1, 5);
b = a;;
endif